package com.haipumi.back.actions;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.haipumi.beans.JsonModel;
import com.haipumi.beans.StatisticalModel;
import com.haipumi.biz.RedisStatisticalBiz;
import com.haipumi.utils.GsonUtil;
import com.opensymphony.xwork2.ActionSupport;

public class statisticalAction extends ActionSupport{

	private static final long serialVersionUID = -8289947299319394784L;
	private JsonModel jm=new JsonModel();
	private String op;
	private String time;
	private RedisStatisticalBiz redisStatisticalBiz;
	
	@Override
	public String execute() {
		if(op!=null){
			if("indexViewsTotal".equals(op)){
				indexViewsTotalOp( );
			}else if("IPViewsTotal".equals(op)){
				IPViewsTotalOp();
			}
		}
		return "success";
	}

	//IP浏览次数统计
	private void IPViewsTotalOp() {
		StatisticalModel model=redisStatisticalBiz.findIPViewsTotal( );
		jm.setCode(1);
		jm.setObj(model);
	}

	//首页浏览次数统计
	private void indexViewsTotalOp() {
		String start;
		String deadline;
		if("当天".equals(time) || time=="" || time==null){
			Date date= new Date();//创建一个时间对象，获取到当前的时间
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//设置时间显示格式
			String str = sdf.format(date);//将当前时间格式化为需要的类型
			start=str;
			deadline=str;
		}else{
			//裁剪传来的字符串,分出开始时间和结束时间
			String[] tdates=time.split("-");
			//开始时间
			String[] tstartDates=tdates[0].split("/");
			if(tstartDates.length<3){//防止空指针
				return;
			}
			//传来的格式 月/日/年
			start="20"+tstartDates[2].trim()+"-"+tstartDates[0].trim()+"-"+tstartDates[1];
			//结束时间
			String[] tendDates=tdates[1].split("/");
			deadline="20"+tendDates[2].trim()+"-"+tendDates[0].trim()+"-"+tendDates[1];
		}
		System.out.println("开始时间"+start+"结束时间"+deadline);
		StatisticalModel model=redisStatisticalBiz.findIndexViewsTotal(start, deadline);
		
		jm.setCode(1);
		jm.setObj(model);
	}

	public JsonModel getJm() {
		return jm;
	}

	public void setJm(JsonModel jm) {
		this.jm = jm;
	}
	
	public void setTime(String time) {
		this.time = time;
	}

	public void setOp(String op) {
		this.op = op;
	}

	@Resource(name = "redisStatisticalBiz")
	public void setRedisStatisticalBiz(RedisStatisticalBiz redisStatisticalBiz) {
		this.redisStatisticalBiz = redisStatisticalBiz;
	}
	
}
